package com.superman.supermarket.client.composite.orderlist;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.orderlist.opt.OrderListOpButton;
import com.superman.supermarket.client.constant.OrderStatusConstants;
import com.superman.supermarket.client.utils.ActivityJumpUrl;

/**
 * 订单列表模块
 * 
 * @author superman
 * @version 2018年8月12日下午2:14:03
 */
public class OrderListModule extends Composite implements IUIModule {

	interface OrderListModuleUiBinder extends UiBinder<Widget, OrderListModule> {
	}

	private static OrderListModuleUiBinder uiBinder = GWT.create(OrderListModuleUiBinder.class);

	@UiField
	HTML createtimehtml, orderstatushtml, payfeehtml;
	@UiField
	FlowPanel mainpanel;
	private String orderId;

	@UiField
	FlowPanel storelistpanel, optpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午2:14:00
	 */
	public OrderListModule() {
		initWidget(uiBinder.createAndBindUi(this));

		mainpanel.addDomHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ActivityJumpUrl.pushToOrderDetailPage(orderId);
			}
		}, ClickEvent.getType());
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.orderId = String.valueOf(map.get("ORDER_ID"));
		String createTimeStr = String.valueOf(map.get("CREATE_TIME"));
		int orderState = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
		double payfee = Double.parseDouble(String.valueOf(map.get("PAY_FEE")));
		createtimehtml.setText(createTimeStr);
		orderstatushtml.setText(OrderStatusConstants.getDescriptionByStatus(orderState));
		payfeehtml.setHTML("实付:¥&nbsp;" + payfee);

		storelistpanel.clear();
		List<Map<String, Object>> list = XJsonUtil.getList((JSONArray) map.get("STORE_ORDER"));
		for (Map<String, Object> storeMap : list) {
			OrderListStoreModule orderListStoreModule = new OrderListStoreModule();
			orderListStoreModule.setData(storeMap);
			this.storelistpanel.add(orderListStoreModule);
		}

		if (map.get("OP") == null || XJsonUtil.getOnlyList((JSONArray) map.get("OP")).size() == 0)
			optpanel.removeFromParent();
		else {
			optpanel.clear();
			List<Object> ops = XJsonUtil.getOnlyList((JSONArray) map.get("OP"));
			for (Object object : ops) {
				int op = (int) Double.parseDouble(String.valueOf(object));
				OrderListOpButton orderListOpButton = new OrderListOpButton(map, op);
				optpanel.add(orderListOpButton);
			}
		}
	}

}
